package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.SysParamPageRequest;
import com.mes.cloud.api.dto.request.SysParamRequest;
import com.mes.cloud.api.dto.response.SysParamDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.dao.SysParamDao;
import com.mes.cloud.entity.SysParam;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统参数表
 *
 * @author Mr.zhang
 * @version 2019-08-02 10:37:18
 * @email zhangchao4@shinho.net.cn
 */
@Service
public class SysParamService {

    private SysParamDao sysParamDao;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public SysParamService(SysParamDao sysParamDao, IPKeyGenerator iPKeyGenerator) {
        this.sysParamDao = sysParamDao;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param pageRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<SysParamDto>> pageList(SysParamPageRequest pageRequest) {
        Page<SysParamDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        List<SysParam> sysParamList = sysParamDao.selectByQueryString(pageRequest.getParentId(), pageRequest.getQueryString(), pageRequest.getEnableFlag());
        List<SysParamDto> sysParamDtoList = new ArrayList<>();
        for (SysParam sysParam : sysParamList) {
            SysParamDto sysParamDto = new SysParamDto();
            BeanUtils.copyProperties(sysParam, sysParamDto);
            sysParamDtoList.add(sysParamDto);
        }
        CommPager<SysParamDto> pageResult = new CommPager<>(page, sysParamDtoList);
        JsonResult<CommPager<SysParamDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 查列表
     *
     * @param sysParamRequest
     * @return JsonResult
     */
    public JsonResult<List<SysParamDto>> list(SysParamRequest sysParamRequest) {
        //查询条件
        SysParam selectParam = new SysParam();
        BeanUtils.copyProperties(sysParamRequest, selectParam);
        List<SysParam> sysParamList = sysParamDao.select(selectParam);
        List<SysParamDto> sysParamDtoList = new ArrayList<>();
        for (SysParam sysParam : sysParamList) {
            SysParamDto sysParamDto = new SysParamDto();
            BeanUtils.copyProperties(sysParam, sysParamDto);
            sysParamDtoList.add(sysParamDto);
        }
        JsonResult<List<SysParamDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysParamDtoList);
        return jsonResult;
    }

    /**
     * 新增
     *
     * @param sysParamRequest
     * @return JsonResult
     */
    public JsonResult insert(SysParamRequest sysParamRequest) {
        JsonResult jsonResult = new JsonResult();
        SysParam sysParam = new SysParam();
        BeanUtils.copyProperties(sysParamRequest, sysParam);
        sysParam.setId(iPKeyGenerator.getGenerateKey());
        sysParam.setUpdateDateStr(DateUtil.updateDateStr());
        sysParam.setCreateBy(UserUtil.getCreateByName());
        sysParam.setUpdateBy(UserUtil.getUpdateByName());
        sysParam.setCreateByNo(UserUtil.getCreateByNo());
        sysParam.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysParamDao.insertSelective(sysParam);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param sysParamRequest
     * @return JsonResult
     */
    public JsonResult update(SysParamRequest sysParamRequest) {
        SysParam sysParam = new SysParam();
        BeanUtils.copyProperties(sysParamRequest, sysParam);
        sysParam.setUpdateDateStr(DateUtil.updateDateStr());
        sysParam.setUpdateBy(UserUtil.getUpdateByName());
        sysParam.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysParamDao.updateByPrimaryKeySelective(sysParam);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult<SysParamDto> get(String id) {
        JsonResult<SysParamDto> jsonResult = new JsonResult<>();
        SysParam sysParam = sysParamDao.selectByPrimaryKey(id);
        if (sysParam == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        SysParamDto sysParamDto = new SysParamDto();
        BeanUtils.copyProperties(sysParam, sysParamDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysParamDto);
    }

    /**
     * 根据code查询
     * @param code
     * @return JsonResult<SysParamDto>
     */
    public JsonResult<SysParamDto> getSysParam(String code) {
        JsonResult<SysParamDto> jsonResult = new JsonResult<>();

        SysParam selectParam = new SysParam();
        selectParam.setCode(code);
        SysParam sysParam = sysParamDao.selectOne(selectParam);
        if (sysParam == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        SysParamDto sysParamDto = new SysParamDto();
        BeanUtils.copyProperties(sysParam, sysParamDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysParamDto);
    }
}
